package top.shiyiri.exer;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Aunean
 * @date 2022/1/9
 */
/*
图片的加密和解密
 */
public class PicTest {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    //图片的加密
    @Test
    public void test1() {

        try {
            fis = new FileInputStream(new File("../data/photo/u2.png"));
            fos = new FileOutputStream(new File("../data/photo/u22.png"));

            byte[] buffer = new byte[20];
            int len;

            while ((len = fis.read(buffer)) != -1) {
                //字节数组进行修改
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fos != null;
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    //图片的解密
    @Test
    public void test2() {

        try {
            fis = new FileInputStream(new File("../data/photo/u22.png"));
            fos = new FileOutputStream(new File("../data/photo/u22_jie.png"));

            byte[] buffer = new byte[20];
            int len;

            while ((len = fis.read(buffer)) != -1) {
                //字节数组进行修改
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fos != null;
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
